/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning South Asian Barometer

--------------------------------------------------------------------------------*/

*South Asian Barometer 1

clear
import spss using "$data_dir/raw/Asia Barometer/SA Wave1/SDSA 2005.sav"

rename v1 country_ab
decode country_ab, gen(country)
replace country = "Sri Lanka" if country=="Srilanka"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "South Asia Barometer"
gen wave = 1

gen weight = .

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if c13a==1|c13a==2
replace trust_govt = 0 if c13a==3|c13a==4

gen trust_justice = 1 if c13g==1|c13g==2
replace trust_justice = 0 if c13g==3|c13g==4

gen trust_police = 1 if c13e==1|c13e==2
replace trust_police = 0 if c13e==3|c13e==4

gen trust_civil_service = 1 if c13d==1|c13d==2
replace trust_civil_service = 0 if c13d==3|c13d==4

gen trust_local_govt = 1 if c13c==1|c13c==2
replace trust_local_govt = 0 if c13c==3|c13c==4

gen trust_parties = 1 if c13i==1|c13i==2
replace trust_parties = 0 if c13i==3|c13i==4

gen trust_parliament = 1 if c13h==1|c13h==2
replace trust_parties = 0 if c13h==3|c13h==4

gen trust_others = .

gen trust_military = 1 if c13f==1|c13f==2
replace trust_military = 0 if c13f==3|c13f==4

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = 2005
clonevar age = d1

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if d2==1
replace sex = 2 if d2==2
replace sex = -99 if d2==.|d2==-1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 2 if inlist(d10, 1)
replace religion = 4 if inlist(d10, 2)
replace religion = 5 if inlist(d10, 6)
replace religion = 7 if inlist(d10, 3)
replace religion = -99 if inlist(d10, 9)
replace religion = 6 if religion==. & d10!=.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = -99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(d7, 20, 21, 22, 23, 26, 30)
replace occupation = 2 if inlist(d7, 1, 2, 3, 4, 5, 6, 12, 13, 14, 15, 16, 18, 24, 31, 39)
replace occupation = 3 if inlist(d7, 7, 8, 9, 10, 19)
replace occupation = 4 if inlist(d7, 11, 25, 27, 29)
replace occupation = 5 if inlist(d7, 32, 33, 35, 36, 37, 40, 42, 44)
replace occupation = 6 if inlist(d7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79)
replace occupation = 7 if inlist(d7, 50, 51, 52, 53, 54, 55, 56, 57, 59)
replace occupation = 8 if inlist(d7, 49, 60, 61, 62, 63, 64)
replace occupation = 9 if inlist(d7, 28, 34, 41, 43, 45, 65, 66, 69)
replace occupation = -99 if inlist(d7, 38, 46, 67, 80, 81, 82, 83, 84, 86, 90, 91, 92, 93, 95, 96, 98, 99)

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if occupation!=-99
replace employment_status = 0 if occupation==-99
replace employment_status = -99 if inlist(d7, 98, 99)

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if d8==1|d8==3
replace marital = 2 if inlist(d8, 2, 4, 5)
replace marital = -99 if inlist(d8, 9)
replace marital = -99 if d8==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if d13==3|d13==4
replace urban = 2 if d13==1|d13==2
replace urban = -99 if d13==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/southasia1_clean.dta", replace

***************************************************************************************************************

*South Asian Barometer 2

clear
import spss using "$data_dir/raw/Asia Barometer/SA Wave2/SDSA 2013.sav"

rename ccode country_ab
decode country_ab, gen(country)
replace country = "Bangladesh" if country=="1: Bangladesh"
replace country = "India" if country=="2: India"
replace country = "Nepal" if country=="4: Nepal"
replace country = "Pakistan" if country=="5: Pakistan"
replace country = "Sri Lanka" if country=="6: Sri Lanka"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "South Asia Barometer"
gen wave = 2

gen weight = .

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if GB7c==1|GB7c==2
replace trust_govt = 0 if GB7c==3|GB7c==4

gen trust_justice = 1 if GB7g==1|GB7g==2
replace trust_justice = 0 if GB7g==3|GB7g==4

gen trust_police = 1 if GB7k==1|GB7k==2
replace trust_police = 0 if GB7k==3|GB7k==4

gen trust_civil_service = 1 if GB7h==1|GB7h==2
replace trust_civil_service = 0 if GB7h==3|GB7h==4

gen trust_local_govt = 1 if GB7f==1|GB7f==2
replace trust_local_govt = 0 if GB7f==3|GB7f==4

gen trust_parties = 1 if GB7i==1|GB7i==2
replace trust_parties = 0 if GB7i==3|GB7i==4

gen trust_parliament = 1 if GB7e==1|GB7e==2
replace trust_parties = 0 if GB7e==3|GB7e==4

gen trust_others = .

gen trust_military = 1 if GB7j==1|GB7j==2
replace trust_military = 0 if GB7j==3|GB7j==4

gen trust_leader = .

pca trust_justice trust_police trust_civil_service trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = 2013
clonevar age = z1
replace age = . if age==328|age==.

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if z5==1
replace sex = 2 if z5==2
replace sex = -99 if z5==.|z5==-1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu
*		  -99   Missing 

gen religion = .
replace religion = 2 if inlist(z10, 5)
replace religion = 4 if inlist(z10, 3)
replace religion = 5 if inlist(z10, 2)
replace religion = 7 if inlist(z10, 1)
replace religion = -99 if inlist(z10, 99)
replace religion = 6 if religion==. & z10!=.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = -99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(z8, 20, 21, 22, 23, 26, 30)
replace occupation = 2 if inlist(z8, 1, 2, 3, 4, 5, 6, 12, 13, 14, 15, 16, 18, 24, 31, 39)
replace occupation = 3 if inlist(z8, 7, 8, 9, 10, 19)
replace occupation = 4 if inlist(z8, 11, 25, 27, 29)
replace occupation = 5 if inlist(z8, 32, 33, 35, 36, 37, 40, 42, 44)
replace occupation = 6 if inlist(z8, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 86, 89)
replace occupation = 7 if inlist(z8, 50, 51, 52, 53, 54, 55, 56, 57, 59)
replace occupation = 8 if inlist(z8, 49, 60, 61, 62, 63, 64)
replace occupation = 9 if inlist(z8, 28, 34, 41, 43, 45, 65, 66, 69, 83)
replace occupation = -99 if inlist(z8, 17, 38, 48, 67, 90, 91, 92, 93, 95, 96, 98, 99, 100, 101, 111)
replace occupation = -99 if z8==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if occupation!=-99
replace employment_status = 0 if occupation==-99
replace employment_status = -99 if inlist(z8, 98, 99)

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if z13==3|z13==4
replace urban = 2 if z13==1|z13==2
replace urban = -99 if z13==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/southasia2_clean.dta", replace
